<template>
    <view>
        <!-- pages/question_type/question_type.wxml -->
        <view class="containView">
            <!-- 列表start -->
            <view class="list-p">
                <view class="list-nav">
                    <block v-for="(item, index) in ['全部', '待回答', '已回答']" :key="item.nav">
                        <view :class="'head-item ' + (index == currentTab ? 'active' : '')" @tap="naviClick" :id="index">{{ item }}</view>
                    </block>
                </view>

                <view class="list-c">
                    <block v-for="(wenItem, index) in datalist" :key="wenItem.wenda">
                        <view class="cell-p" @tap="cellClick" :id="index">
                            <block v-if="wenItem.imageA.length == 1">
                                <view class="cell-single">
                                    <view class="cell-single-left">
                                        <!-- parse <template is="answer_cell" :data="wenItem,minImgDoc"/> -->
                                        <block name="answer_cell">
                                            <view class="cell">
                                                <!-- cell标题 start -->
                                                <view :class="'cell-title ' + (wenItem.imageA.length == 1 ? 'single-image' : '')">
                                                    <text class="wen">问</text>
                                                    {{ wenItem.subject }}
                                                </view>
                                                <!-- cell标题 end -->

                                                <!-- 附件 start -->
                                                <view class="cell-att" v-if="wenItem.imageA.length > 1">
                                                    <block v-if="index < 3" v-for="(item, index) in wenItem.imageA" :key="item.cellAtt">
                                                        <image class="cell-image" :src="item.attachment" mode="aspectFill"></image>
                                                    </block>
                                                </view>

                                                <!-- 附件 end -->

                                                <!-- cell信息start -->
                                                <view class="cell-info">
                                                    <view class="cell-info-left">
                                                        <view class="icon-item cell-tag">
                                                            <image class="cell-icon" :src="minImgDoc + 're_tag.png'"></image>
                                                            <view class="cell-icon-text">{{ wenItem.typename ? wenItem.typename : '分类' }}</view>
                                                        </view>
                                                        <view class="icon-item cell-recount">
                                                            <image class="cell-icon" :src="minImgDoc + 're_dis.png'"></image>
                                                            <view class="cell-icon-text">{{ wenItem.replies }}个回答</view>
                                                        </view>
                                                    </view>

                                                    <view cell-info-right>
                                                        <view class="icon-item cell-recount" @tap.stop.prevent="clickReply" :id="wenItem.tid">
                                                            <image class="cell-icon" :src="minImgDoc + 're_re.png'"></image>
                                                            <view class="cell-reply">回答</view>
                                                        </view>
                                                    </view>
                                                </view>
                                            </view>
                                            <!-- cell信息end -->
                                        </block>
                                    </view>
                                    <view class="cell-single-right">
                                        <image class="cell-image" :src="wenItem.imageA[0].attachment"></image>
                                    </view>
                                </view>
                            </block>
                            <block v-else>
                                <!-- parse <template is="answer_cell" :data="wenItem,minImgDoc"/> -->
                                <block name="answer_cell">
                                    <view class="cell">
                                        <!-- cell标题 start -->
                                        <view :class="'cell-title ' + (wenItem.imageA.length == 1 ? 'single-image' : '')">
                                            <text class="wen">问</text>
                                            {{ wenItem.subject }}
                                        </view>
                                        <!-- cell标题 end -->

                                        <!-- 附件 start -->
                                        <view class="cell-att" v-if="wenItem.imageA.length > 1">
                                            <block v-if="index < 3" v-for="(item, index) in wenItem.imageA" :key="item.cellAtt">
                                                <image class="cell-image" :src="item.attachment" mode="aspectFill"></image>
                                            </block>
                                        </view>

                                        <!-- 附件 end -->

                                        <!-- cell信息start -->
                                        <view class="cell-info">
                                            <view class="cell-info-left">
                                                <view class="icon-item cell-tag">
                                                    <image class="cell-icon" :src="minImgDoc + 're_tag.png'"></image>
                                                    <view class="cell-icon-text">{{ wenItem.typename ? wenItem.typename : '分类' }}</view>
                                                </view>
                                                <view class="icon-item cell-recount">
                                                    <image class="cell-icon" :src="minImgDoc + 're_dis.png'"></image>
                                                    <view class="cell-icon-text">{{ wenItem.replies }}个回答</view>
                                                </view>
                                            </view>

                                            <view cell-info-right>
                                                <view class="icon-item cell-recount" @tap.stop.prevent="clickReply" :id="wenItem.tid">
                                                    <image class="cell-icon" :src="minImgDoc + 're_re.png'"></image>
                                                    <view class="cell-reply">回答</view>
                                                </view>
                                            </view>
                                        </view>
                                    </view>
                                    <!-- cell信息end -->
                                </block>
                            </block>
                        </view>
                    </block>
                </view>
            </view>
            <!-- 列表end -->
            <view class="noMoreView" v-if="noMore">别再用力了，你已经滑到底啦</view>
        </view>

        <image :src="minImgDoc + 'course_post.png'" class="postEnter borderShowdow" @tap="postEnter">发布</image>
        <block name="answer_cell">
            <view class="cell">
                <!-- cell标题 start -->
                <view :class="'cell-title ' + (wenItem.imageA.length == 1 ? 'single-image' : '')">
                    <text class="wen">问</text>
                    {{ wenItem.subject }}
                </view>
                <!-- cell标题 end -->

                <!-- 附件 start -->
                <view class="cell-att" v-if="wenItem.imageA.length > 1">
                    <block v-if="index < 3" v-for="(item, index) in wenItem.imageA" :key="item.cellAtt">
                        <image class="cell-image" :src="item.attachment" mode="aspectFill"></image>
                    </block>
                </view>

                <!-- 附件 end -->

                <!-- cell信息start -->
                <view class="cell-info">
                    <view class="cell-info-left">
                        <view class="icon-item cell-tag">
                            <image class="cell-icon" :src="minImgDoc + 're_tag.png'"></image>
                            <view class="cell-icon-text">{{ wenItem.typename ? wenItem.typename : '分类' }}</view>
                        </view>
                        <view class="icon-item cell-recount">
                            <image class="cell-icon" :src="minImgDoc + 're_dis.png'"></image>
                            <view class="cell-icon-text">{{ wenItem.replies }}个回答</view>
                        </view>
                    </view>

                    <view cell-info-right>
                        <view class="icon-item cell-recount" @tap.stop.prevent="clickReply" :id="wenItem.tid">
                            <image class="cell-icon" :src="minImgDoc + 're_re.png'"></image>
                            <view class="cell-reply">回答</view>
                        </view>
                    </view>
                </view>
            </view>
            <!-- cell信息end -->
        </block>
    </view>
</template>

<script>
// pages/question_type/question_type.js
const forumdisplayUrl = require('../../config').forumdisplayUrl;

const minImgDoc = require('../../config').minImgDoc;

const util = require('../../utils/util.js');

const app = getApp();

export default {
    data() {
        return {
            minImgDoc: minImgDoc,
            fid: 50,
            currentTab: 0,
            singleimage: true,
            typeid: '',
            page: 1,
            datalist: [],
            threadtypes: {},
            noMore: '',
            dataDic: '',

            wenItem: {
                wenda: '',
                imageA: [],
                typename: false,
                replies: '',
                tid: ''
            }
        };
    },
    onShow: function () {

    },
    onLoad: function (options) {
        console.log('执行了');


        if (options.typeid) {
            this.typeid= options.typeid;
        }

        this.fourmdisplayRequest();
    },
    onPullDownRefresh: function () {
        this.page = 1;
        this.fourmdisplayRequest();
    },
    onReachBottom: function () {
        if (!this.noMore) {
            this.page++;
            this.fourmdisplayRequest();
        }
    },
    methods: {
        refreshRequest() {
            this.page= 1;
            this.fourmdisplayRequest();
        },

        fourmdisplayRequest() {
            var data = {};

            if (this.currentTab == 0) {
                data = {
                    fid: this.fid,
                    page: this.page,
                    mobile_api: 1
                };
            } else {
                if (this.currentTab == 1) {
                    data = {
                        filter: 'specialtype',
                        specialtype: 'reward',
                        rewardtype: 1,
                        fid: this.fid,
                        page: this.page,
                        mobile_api: 1
                    };
                } else {
                    if (this.currentTab == 2) {
                        data = {
                            filter: 'specialtype',
                            specialtype: 'reward',
                            rewardtype: 2,
                            fid: this.fid,
                            page: this.page,
                            mobile_api: 1
                        };
                    }
                }
            }

            if (this.typeid) {
                data['filter'] = 'typeid';
                data['typeid'] = this.typeid;
            }

            app.globalData.apimanager
                .getRequest(forumdisplayUrl, data)
                .then((res) => {
                    uni.stopPullDownRefresh();
                    var usernicknames = res.Variables.usernicknames;
                    let arr1 = res.Variables.forum_threadlist;

                    if (res.Variables.threadtypes) {
                        this.threadtypes= res.Variables.threadtypes;

                        if (this.typeid) {
                            uni.setNavigationBarTitle({
                                title: this.threadtypes.types[this.typeid]
                            });
                        }
                    }

                    for (let i = 0; i < arr1.length; i++) {
                        let postItem = arr1[i];

                        if (usernicknames) {
                            if (usernicknames[postItem.authorid]) {
                                postItem.nickname = usernicknames[postItem.authorid];
                            }
                        }

                        if (this.threadtypes && postItem.typeid > 0) {
                            postItem['typename'] = this.threadtypes.types[postItem.typeid];
                        }

                        if (postItem.message && postItem.message.length > 0) {
                            postItem.message = util.filterHtml(postItem.message);
                        }

                        let attachments = postItem.attachlist;

                        if (attachments) {
                            let imageA = [];

                            for (let k = 0; k < attachments.length; k++) {
                                let attItem = attachments[k];
                                let realIndex = i;

                                if (this.page > 1) {
                                    realIndex = i + this.datalist.length;
                                }

                                if (attItem.type == 'image') {
                                    attItem['toolUse'] = {
                                        listIndex: realIndex,
                                        imageIndex: k
                                    };
                                    imageA.push(attItem);
                                }
                            }

                            postItem['imageA'] = imageA;
                        }
                    }

                    if (this.page > 1 && arr1.length > 0) {
                        arr1 = this.datalist.concat(arr1);
                    }

                    var noMore = false;

                    if (arr1.length >= res.Variables.forum.threadcount) {
                        noMore = true;
                    }

                    this.setData({
                        datalist: arr1,
                        noMore: noMore,
                        dataDic: res
                    });
                })
                .catch((res) => {
                    uni.stopPullDownRefresh();
                    console.log(res);
                });
        },

        naviClick(e) {
            this.currentTab= e.currentTarget.id;this.page= 1;
            this.fourmdisplayRequest();
        },

        cellClick(e) {
            var tid = this.datalist[e.currentTarget.id].tid;
            uni.navigateTo({
                url: '../question_answer_detail/question_answer_detail?tid=' + tid
            });
        },

        clickReply(e) {
            uni.navigateTo({
                url: '../post_thread/post_thread?isreply=true&tid=' + e.currentTarget.id + '&fid=' + this.fid
            });
        },

        postEnter() {
            uni.navigateTo({
                url: '../question_answer_post/question_answer_post?fid=' + this.fid + '&typeid=' + this.typeid
            });
        }
    }
};
</script>
<style>
@import './question_type.css';
</style>
